<?php
/**
 * Created by PhpStorm.
 * User: Core
 * Date: 2018/3/13
 * Time: 9:43
 */
namespace App\Libs;

class ApiMessage
{
    //状态码定义
    const SUCCESS = 200;
    const FAIL = 5000;

    private $code;
    private $message;
    private $data;

    public function __construct($code=200,$message="",$data="")
    {
        $this->code = $code;
        $this->message = $message;
        $this->data = $data;
    }

    /**
     * @return mixed
     */
    public function getCode()
    {
        return $this->code;
    }

    /**
     * @param mixed $code
     */
    public function setCode($code)
    {
        $this->code = $code;
    }

    /**
     * @return mixed
     */
    public function getMessage()
    {
        return $this->message;
    }

    /**
     * @param mixed $message
     */
    public function setMessage($message)
    {
        $this->message = $message;
    }

    /**
     * @return mixed
     */
    public function getData()
    {
        return $this->data;
    }

    /**
     * @param mixed $data
     */
    public function setData($data)
    {
        $this->data = $data;
    }

    public function send(){
        $array = [
            'code' => $this->code,
            'message' => $this->message ? $this->message :
                ($this->code == self::SUCCESS ? "request success" : "request fail"),
            'data' => $this->data
        ];
        return  json_encode($array);
    }
}